//
//  HomeTableViewCell.m
//  shanHaiHuiApp
//
//  Created by DS on 2018/3/6.
//  Copyright © 2018年 徐双. All rights reserved.
//

#import "HomeTableViewCell.h"
#import "UIImage+GIF.h"
#import "SDWebImageGIFCoder.h"
#import "NSImage+WebCache.h"



@interface HomeTableViewCell()<UITextFieldDelegate>



@end

@implementation HomeTableViewCell

# pragma mark - Action

- (void)minusBtnClick{
 
    if (_getMinusClick) {
        _getMinusClick(self,self.textField);
    }
 
}

- (void)plusBtnClick{
 
 
        if (_getPlusClick) {
            _getPlusClick(self,self.textField,self.piciamgeView,self.plusBtn);
        }
 
}

- (void)textFieldClick{
    
    if (_getTextFieldClick) {
        _getTextFieldClick(self,self.textField);
    }
    
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    return [textField resignFirstResponder];
}


# pragma mark - Init
- (id)initWithCellIdentifier:(NSString *)cellID{
    if (self = [super initWithCellIdentifier:cellID]) {
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        
        _piciamgeView = [UIImageView new];
        _piciamgeView.layer.masksToBounds = YES;
        _piciamgeView.image = [UIImage imageNamed:@"shanhaihuilogo"];
      
        
        _goodLabel = [[UILabel alloc] init];
        _goodLabel.font = DEF_FontSize_15;
        _goodLabel.textColor = getColor(@"323232");
        _goodLabel.textAlignment = NSTextAlignmentLeft;
       // _goodLabel.text = @"三只松鼠";
       
        
        _pariceLabel = [[UILabel alloc] init];
        _pariceLabel.font = DEF_FontSize_17;
        _pariceLabel.textColor = getColor(@"EC4350");
       // _pariceLabel.text = @"￥99.6";
        
        _perLabel = [[UILabel alloc] init];
        _perLabel.font = DEF_FontSize_12;
        _perLabel.textColor = getColor(@"666666");
       // _perLabel.text = @"/￥99.6";
        
        _minusBtn = [[UIButton alloc] init];
        [_minusBtn setImage:[UIImage imageNamed:@"jian"] forState:UIControlStateNormal];
        [_minusBtn addTarget:self action:@selector(minusBtnClick) forControlEvents:UIControlEventTouchUpInside];
        
        _plusBtn = [[UIButton alloc] init];
        [_plusBtn setImage:[UIImage imageNamed:@"jia"] forState:UIControlStateNormal];
        [_plusBtn addTarget:self action:@selector(plusBtnClick) forControlEvents:UIControlEventTouchUpInside];
        
        _textField = [[UITextField alloc] init];
        _textField.font = DEF_FontSize_14;
        _textField.textColor = getColor(@"323232");
        //_textField.text = @"2";
        _textField.textAlignment = NSTextAlignmentCenter;
        _textField.layer.borderWidth = 1.0f;
        _textField.layer.borderColor = [UIColor grayColor].CGColor;
        _textField.keyboardType =  UIKeyboardTypeNumberPad;
        _textField.delegate = self;
        //去调用上面的代码，给这个UITextField的对象添加UIToolbar
        _textField.inputAccessoryView = [self addToolbar];
        [_textField addTarget:self action:@selector(textFieldClick) forControlEvents:UIControlEventEditingDidEnd];
        
        _lineView = [[UIView alloc] init];
        _lineView.backgroundColor = getColor(dividerColor);
        
       
       
        [self sd_addSubviews:@[self.piciamgeView,self.goodLabel,self.pariceLabel,self.perLabel,self.minusBtn,self.plusBtn,self.textField,self.lineView]];
        
        [self layOutFrame];
        
    }
    return self;
}

- (void)layOutFrame{
    ws(self)
    [_piciamgeView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(UNIT_HEIGHT(15));
        make.left.mas_equalTo(UNIT_WIDTH(15));
        make.height.mas_equalTo(UNIT_WIDTH(75));
        make.width.mas_equalTo(UNIT_HEIGHT(75));
    }];
    
    [_goodLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(UNIT_HEIGHT(15));
        make.left.mas_equalTo(weakself.piciamgeView.mas_right).offset(UNIT_WIDTH(21));
        make.right.mas_equalTo(UNIT_WIDTH(-27));
    }];
    
    [_pariceLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(weakself.piciamgeView.mas_right).offset(UNIT_WIDTH(21));
        make.bottom.mas_equalTo(weakself.piciamgeView.mas_bottom);
        make.height.mas_equalTo(UNIT_HEIGHT(14));
    }];
    
    [_perLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(weakself.pariceLabel.mas_right).offset(0);
        make.top.mas_equalTo(weakself.pariceLabel.mas_top);
        make.bottom.mas_equalTo(weakself.pariceLabel.mas_bottom);
    }];
    
    [_plusBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(UNIT_WIDTH(-15));
        make.bottom.mas_equalTo(weakself.piciamgeView.mas_bottom);
        make.width.mas_equalTo(UNIT_WIDTH(30));
        make.height.mas_equalTo(UNIT_HEIGHT(30));
    }];
    
    [_textField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(weakself.plusBtn.mas_left).offset(0);
        make.bottom.mas_equalTo(weakself.plusBtn.mas_bottom);
        make.top.mas_equalTo(weakself.plusBtn.mas_top);
        make.width.mas_equalTo(50);
    }];
    
    [_minusBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(weakself.textField.mas_left).offset(0);
        make.bottom.mas_equalTo(weakself.piciamgeView.mas_bottom);
        make.width.mas_equalTo(UNIT_WIDTH(30));
        make.height.mas_equalTo(UNIT_HEIGHT(30));
    }];
    
    [self.lineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.mas_equalTo(0);
        make.left.mas_equalTo(UNIT_WIDTH(10));
        make.right.mas_equalTo(UNIT_WIDTH(-10));
        make.height.mas_equalTo(UNIT_HEIGHT(1));
    }];
}



- (void)setModel:(HotGoodModel *)model{
    _model = model;
    
    
    
//    NSString *path = [[NSBundle mainBundle] pathForResource:@"3e07e0a7-0fea-4ca3-b7b2-3896302c9270" ofType:@"gif"];
//    NSData *data = [NSData dataWithContentsOfFile:path];
//    UIImage *image = [UIImage sd_animatedGIFWithData:data];
//    self.piciamgeView.image = image;
    
    [[SDWebImageDownloader sharedDownloader] setValue:@"text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8" forHTTPHeaderField:@"Accept"];
    [self.piciamgeView sd_setImageWithURL:[NSURL URLWithString:model.shImagesfilename]];
    self.perLabel.text = [NSString stringWithFormat:@"/￥%2@",model.fDj2];
    self.pariceLabel.text = [NSString stringWithFormat:@"￥%2@",model.fDj2];
    self.goodLabel.text = model.fSpmc;
    
}


- (UIToolbar *)addToolbar
{
    
    UIToolbar *toolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 50)];
    
    UIBarButtonItem *space = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
    
    UIBarButtonItem *bar = [[UIBarButtonItem alloc] initWithTitle:@"完成" style:UIBarButtonItemStylePlain target:self action:@selector(numberFieldCancle)];
    
    
    toolbar.items = @[space, bar];
    
    return toolbar;
}

- (void)numberFieldCancle{
    
    [self.textField resignFirstResponder];
    
}



@end
